﻿/*using System;

namespace HW2_b4
{
    abstract class TwoDShape{
        public abstract double Area();
        public abstract void Initialization();

    }
    class Square: TwoDShape
    {
        double width;
        public Square(double width)
        {
            Initialization();
        }
        public override double Area()
        {
            return width * width;
            throw new NotImplementedException();
        }
        public override void Initialization()
        {
            while (true)
            {
                Console.WriteLine("请输入正方形的边长");
                string wid_str = Console.ReadLine();
                if (!double.TryParse(wid_str, out width))
                {
                    Console.WriteLine("输入格式有误，请输入数字");
                    continue;
                }
                break;
                throw new NotImplementedException();
            }

        }
    }
    class Rectangle: TwoDShape
    {
        double width;
        double height;
        public Rectangle(double width, double height)
        {
            Initialization();
        }
        public override double Area()
        {
            return width * height;
            throw new NotImplementedException();
        }
        public override void Initialization()
        {
            while (true)
            {
            Console.WriteLine("请输入长方形的宽");
            string wid_str = Console.ReadLine();
            Console.WriteLine("请输入长方形的长");
            string hei_str = Console.ReadLine();
            if (!(double.TryParse(wid_str, out width) && double.TryParse(hei_str, out height)))
            {
                Console.WriteLine("输入格式有误，请输入数字");
                continue;
            }
            break;
            throw new NotImplementedException();
            }
            
        }
    }
    class Triangle : TwoDShape
    {
        double side1;
        double side2;
        double side3;
        public Triangle(double side1, double side2,double side3)
        {
            Initialization();
        }
        public override double Area()
        {
            double CosB = (side1 * side1 + side3 * side3 - side2 * side2) / (2 * side3 * side1);
            double SinB = System.Math.Sqrt(1 - CosB * CosB);
            return 0.5 * side1 * side3 * SinB;
            throw new NotImplementedException();
        }

        public override void Initialization()
        {
            while (true)
            {
                Console.Write("请输入三角形第1个边长：");
                string side1Str = Console.ReadLine();
                Console.Write("请输入三角形第2个边长：");
                string side2Str = Console.ReadLine();
                Console.Write("请输入三角形第3个边长：");
                string side3Str = Console.ReadLine();
                if (!double.TryParse(side1Str, out side1) || !double.TryParse(side2Str, out side2)
                         || !double.TryParse(side3Str, out side3))
                {
                    Console.WriteLine("您输入的数据出现非法字符，请重新输入！");
                    continue;
                }
                if(side1+side2<side3|| side1 + side3 < side2 ||side3 + side2 < side1)
                {
                    Console.WriteLine("您输入的数据无法构成三角形，请重新输入！");
                    continue;
                }
                break;
            }
            throw new NotImplementedException();

        }
    }

    class EasyFactory
    {
        public static TwoDShape CreateFunction(string cate,double width,double height,double side1, double side2, double side3)
        {
            switch (cate)
            {
                case "1": return new Square(width);
                case "2": return new Rectangle(width, height);
                case "3": return new Triangle(side1, side2, side3);
                default: return null;
            }    
        }
    }
    static void Main(string[] args)
        {
            Console.WriteLine("请选择您要计算的形状，如下：");
            Console.WriteLine("1、正方形 2、长方形 3、三角形");
            string name = Console.ReadLine();
            TwoDShape tdshape = EasyFactory.CreateFunction(name);
        }
}
*/


using System;
using System.Threading;


namespace HW2_b4
{
    abstract class Shape
    {
        protected double edge1, edge2, edge3;
        protected double area;
        public abstract bool legal();
    }
    public interface Compute
    {
        double areaCompute();
    }
    class Square : Rectangle, Compute
    {
        public Square(double l1) : base(l1, 0)
        {
            edge1 = edge2 = edge3 = edge4 = l1;
        }
        public override bool legal()
        {
            if (edge1 > 0) return true;
            return false;
        }
        double Compute.areaCompute()
        {
            if (!legal()) return -1;
            area = edge1 * edge1; return area;
        }
    }
    class Rectangle : Shape, Compute
    {
        protected double edge4;
        public Rectangle(double l1, double l2)
        {
            edge1 = edge2 = l1; edge3 = edge4 = l2;
        }
        public override bool legal()
        {
            if (edge1 > 0 && edge3 > 0) return true;
            return false;
        }
        double Compute.areaCompute()
        {
            if (!legal())
            {
                return -1;
            }

            area = edge1 * edge3; return area;
        }
    }
    class Triangle : Shape, Compute
    {
        public Triangle(double l1, double l2, double l3)
        {
            edge1 = l1; edge2 = l2; edge3 = l3;
        }
        double Compute.areaCompute()
        {
            if (!legal()) return -1;
            double s = (edge1 + edge2 + edge3) / 2;
            double area = Math.Sqrt(s * (s - edge1) * (s - edge2) * (s - edge3));
            return area;
        }
        public override bool legal()
        {
            if ((edge1 + edge2 <= edge3) || (edge1 + edge3 <= edge2) || (edge3 + edge2 <= edge1)) return false;
            return true;
        }
    }
    public class ShapeFactory
    {
        public static Compute CreateShape(string type, double l1, double l2, double l3, double l4 = 0)
        {
            switch (type.ToLower())
            {
                case "square":
                    return new Square(l1);
                case "rectangle":
                    return new Rectangle(l1, l2);
                case "triangle":
                    return new Triangle(l1, l2, l3);
                default:
                    throw new ArgumentException($"未知类型：{type}");
            }
        }
        public static Compute CreateRandomShape()
        {
            Random random = new Random();
            int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;
            Thread.Sleep(10);
            random = new Random(seed);
            int randomNumber = random.Next(1, 4); // 随机数生成范围是 [1, 3]
            int ran1 = random.Next(1, 11); // 随机数生成范围是 [1, 9]
            int ran2 = random.Next(1, 11);
            switch (randomNumber)
            {
                case 1:
                    Compute s1 = CreateShape("square", ran1, ran1, ran1, ran1);
                    System.Console.WriteLine("generate a square {0}", ran1);
                    return s1;

                case 2:
                    Compute s2 = CreateShape("rectangle", ran1, ran2, ran1, ran2);

                    System.Console.WriteLine("generate a rectangle {0}, {1}", ran1, ran2);
                    return s2;
                case 3:
                    int ran3 = random.Next(1, 11);
                    Compute s3 = CreateShape("triangle", ran1, ran2, ran3);

                    System.Console.WriteLine("generate a triangle {0}, {1}, {2}", ran1, ran2, ran3);
                    return s3;
                default:
                    return null;
            }
        }
    }

    public class process
    {
        static void Main(string[] args)
        {
            double areaSum = 0;

            for (int i = 0; i < 10; i++)
            {
                Compute s = ShapeFactory.CreateRandomShape();
                areaSum += s.areaCompute();
            }

            System.Console.WriteLine("以下为10个形状面积之和");
            System.Console.WriteLine(areaSum);

        }
    }
}


